import socket
import threading
import sys

lock = threading.Lock()


def do(port):
    threads = []
    for i in range(1, 255):
        ip = '192.168.1.%d' % i
        t = threading.Thread(target=check_ip, args=(ip, port))
        t.start()
        threads.append(t)
    for t in threads:
        t.join()


def check_ip(ip, port):
    scan_link = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    scan_link.settimeout(2)
    result = scan_link.connect_ex((ip, port))
    scan_link.close()

    if result == 0:
        lock.acquire()
        print(ip, '\t\t端口 %s 开放' % port)
        lock.release()


if __name__ == '__main__':
    do(int(sys.argv[1]))
